clear all;

cd ..; cd ..; cd ..;
load('code_main_model\6_implied_w_r\computation_results\data.mat','Z','adjust_backward','gl_backward','gk_backward','nmkt','pdf_B_forward','GDP','K','Y','chi','alpha','nu','psi',...
    'nb','ntheta','nz','gridb','gridtheta','gridz','nb_pdf','ntheta_pdf','gridb_pdf','gridtheta_pdf','period_trans');
% load('code_main_model_credit\6_implied_w_r\computation_results\data.mat','Z','adjust_backward','gl_backward','gk_backward','nmkt','pdf_B_forward','GDP','K','Y','chi','alpha','nu','psi',...
%     'nb','ntheta','nz','gridb','gridtheta','gridz','nb_pdf','ntheta_pdf','gridb_pdf','gridtheta_pdf','period_trans');
cd 'code_figure_table\OA\Figure OA29';

KK(1:period_trans-1)=0.0;
YY(1:period_trans-1)=0.0;
GDP1(1:period_trans-1)=0.0;

temp_fin(1:period_trans-1, 1:nmkt, 1:nz, 1:nb, 1:ntheta)=0.0;
for i_t=1:period_trans-1
    for i_mkt=1:nmkt
        for i_b=1:nb
            for i_theta=1:ntheta
                for i_z=1:nz
                    if (adjust_backward(i_t, i_mkt, i_z, i_b, i_theta)==1 || adjust_backward(i_t, i_mkt, i_z, i_b, i_theta)==2)
                        temp_fin(i_t, i_mkt, i_z, i_b, i_theta)=0.0;
                    elseif (adjust_backward(i_t, i_mkt, i_z, i_b, i_theta)==3 || adjust_backward(i_t, i_mkt, i_z, i_b, i_theta)==4)
                        temp_fin(i_t, i_mkt, i_z, i_b, i_theta)=0.0;    
                    elseif (adjust_backward(i_t, i_mkt, i_z, i_b, i_theta)==5 || adjust_backward(i_t, i_mkt, i_z, i_b, i_theta)==6)
                        temp_fin(i_t, i_mkt, i_z, i_b, i_theta)=1.0;    
                    end
                end
            end
        end
    end
end

for i_t=1:period_trans-1
    for i_mkt=1:nmkt
        for i_b=1:nb_pdf
            for i_theta=1:ntheta_pdf
                for i_z=1:nz
                    if (pdf_B_forward(i_t,i_mkt,i_z,i_b,i_theta)~=0.0)

                        k=lin_interpo2(nb, ntheta, gridb, gridtheta, squeeze(gk_backward(i_t,i_mkt,i_z,:,:)), gridb_pdf(i_b), gridtheta_pdf(i_theta));
                        KK(i_t)=KK(i_t)+k*pdf_B_forward(i_t,i_mkt,i_z,i_b,i_theta);
                        labor=lin_interpo2(nb, ntheta, gridb, gridtheta, squeeze(gl_backward(i_t,i_mkt,i_z,:,:)), gridb_pdf(i_b), gridtheta_pdf(i_theta));
                        if ((k<1e-10) || (labor<1e-10))
                            k=0;
                            labor=0;
                        end
                        y=Z(i_mkt)*gridz(i_z)*(k^alpha*labor^(1-alpha))^(1-nu);
                        YY(i_t)=YY(i_t)+y*pdf_B_forward(i_t,i_mkt,i_z,i_b,i_theta);
                        
                        fin= lin_interpo2(nb, ntheta, gridb, gridtheta, squeeze(temp_fin(i_t,i_mkt,i_z,:,:)), gridb_pdf(i_b), gridtheta_pdf(i_theta))*psi(i_t,i_mkt);

                        b=gridb_pdf(i_b);
                        theta=gridtheta_pdf(i_theta);
                        m=b*theta;
                        if (k>m)
                            borrowing=k+psi(i_t,i_mkt)-m;
                        else
                            borrowing=0.0;
                        end
                        GDP1(i_t)=GDP1(i_t)+(y-fin-borrowing*chi)*pdf_B_forward(i_t,i_mkt,i_z,i_b,i_theta);

                       
                    end
                end
            end
        end
    end
end

prod(1:period_trans-1)=0.0;
counter(1:period_trans-1)=0.0;
for i_t=1:period_trans-1
    for i_mkt=1:nmkt
        for i_b=1:nb_pdf
            for i_theta=1:ntheta_pdf
                for i_z=1:nz
                    if (pdf_B_forward(i_t,i_mkt,i_z,i_b,i_theta)~=0.0)
                        k=lin_interpo2(nb, ntheta, gridb, gridtheta, squeeze(gk_backward(i_t,i_mkt,i_z,:,:)), gridb_pdf(i_b), gridtheta_pdf(i_theta));
                        labor=lin_interpo2(nb, ntheta, gridb, gridtheta, squeeze(gl_backward(i_t,i_mkt,i_z,:,:)), gridb_pdf(i_b), gridtheta_pdf(i_theta));
                        if ((k<1e-10) || (labor<1e-10))
                            k=0;
                            labor=0;
                        end
                        y=Z(i_mkt)*gridz(i_z)*(k^alpha*labor^(1-alpha))^(1-nu);
                        fin=lin_interpo2(nb, ntheta, gridb, gridtheta, squeeze(temp_fin(i_t,i_mkt,i_z,:,:)), gridb_pdf(i_b), gridtheta_pdf(i_theta))*psi(i_t,i_mkt);
                        
                        b=gridb_pdf(i_b);
                        theta=gridtheta_pdf(i_theta);
                        m=b*theta;
                        if (k>m)
                            borrowing=k+psi(i_t,i_mkt)-m;
                        else
                            borrowing=0.0;
                        end
                        prod(i_t)=prod(i_t)+gridz(i_z)*(y-fin-borrowing*chi)/GDP1(i_t)*pdf_B_forward(i_t,i_mkt,i_z,i_b,i_theta);
                        counter(i_t)=counter(i_t)+(y-fin-borrowing*chi)/GDP1(i_t)*pdf_B_forward(i_t,i_mkt,i_z,i_b,i_theta);
                    end
                end
            end
        end
    end
end
prod_baseline=prod(1:11);
save('baseline.mat','prod_baseline');
% save('credit.mat','prod_psi');
% save('deposit.mat','prod_zeta');
% save('noexp.mat','prod_no_reform');
